rm(list=ls())
gc()
library(tictoc)
library(data.table)
library(tidyverse)
library(stringr)
library(gt)
# Note: Running all the scripts takes approximately 30 hours on a single core, 
# and requires memory space of approximately 100gb-150gb, given the size of the data.

con <- file("run_all.log")
sink(con, append=TRUE)
sink(con, append=TRUE, type="message")

tic() # time running all of the code

# print current version of R and print current date and time
getRversion()
print(Sys.time())

# create directories to store output
if(!dir.exists('results')){
  dir.create("results")
}

if(!dir.exists('results/sds')){
  dir.create("results/sds")
}

if(!dir.exists('figures')){
  dir.create("figures")
}

if(!dir.exists('tables')){
  dir.create("tables")
}



# Manuscript Figures and Tables


# Figure 1 -- No code for figure 1 in repository. Maps require exact residential address information which is excluded from the repository to protect voter privacy.


# Figure 2
tic() # time scripts for each figure
source('FIG-party-switching-matrix.R', echo = T,  max.deparse.length=10000)
rm(list=ls())
gc()
toc()
gc()

# Figure 3
tic() # time scripts for each figure
source('FIG-distributions.R', echo = T,  max.deparse.length=10000)
rm(list=ls())
gc()
toc()
gc()

# Figure 4
tic() # time scripts for each figure
source('EST-current-results-2008-2012.R', echo = T,  max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-current-results-2012-2016.R', echo = T,  max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-current-results-2016-2020.R', echo = T,  max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-current-results-2012-2016-pretrend.R', echo = T,  max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-current-results-2016-2020-pretrend.R', echo = T,  max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-future-results-2008-2012.R', echo = T,  max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-future-results-2012-2016.R', echo = T,  max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-future-results-2012-2016-pretrend.R', echo = T,  max.deparse.length=10000)
rm(list=ls())
gc()
source('FIG-current-results.R', echo = T,  max.deparse.length=10000)
rm(list=ls())
gc()
source('FIG-future-results.R', echo = T,  max.deparse.length=10000)
rm(list=ls())
gc()
toc()
gc()

# Figure 5
tic() # time scripts for each figure
source('EST-placebo-results-2016-2020.R', echo = T,  max.deparse.length=10000)
rm(list=ls())
gc()
source('FIG-placebo.R', echo = T,  max.deparse.length=10000)
toc()
gc()

# Figure 6 (also creates Figure S2)
tic() # time scripts for each figure
source('EST-current-results-2016-2020-dwelling-age.R', echo = T,  max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-current-results-2012-2016-dwelling-age.R', echo = T,  max.deparse.length=10000)
rm(list=ls())
gc()
source('FIG-current-results-age-dwelling.R', echo = T,  max.deparse.length=10000)
toc()
gc()

# Figure 7 (also creates Figure S6)
tic() # time scripts for each figure
source('EST-current-results-2016-2020-race-subset.R', echo = T,  max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-current-results-2012-2016-race-subset.R', echo = T,  max.deparse.length=10000)
rm(list=ls())
gc()
source('FIG-current-results-race-subset.R', echo = T,  max.deparse.length=10000)
toc()
gc()

# Figure 8 (also creates Figure S)
tic() # time scripts for each figure
source('EST-current-results-2016-2020-by-district.R', echo = T,  max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-current-results-2012-2016-by-district.R', echo = T,  max.deparse.length=10000)
rm(list=ls())
gc()
source('FIG-current-results-districts.R', echo = T,  max.deparse.length=10000)
toc()
gc()

# Table 1 - No code for Table 1 in repository -- Table does not use data and consists of a description of survey measures

# Table 2
tic() # time scripts for each figure
source('EST-survey-analysis-weights.R', echo = T,  max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-survey-analysis-no-weights.R', echo = T,  max.deparse.length=10000)
rm(list=ls())
gc()
source('TAB-survey-results.R', echo = T,  max.deparse.length=10000)
toc()
gc()


# Supporting Information Figures and Tables

# Table S1 - No code for Table S1 in repository -- Table does not use data and consists of a description of for which states and years the project has voter file data


# Table S2
tic() # time scripts for each figure
source('TAB-linked-unlinked-table.R', echo = T,  max.deparse.length=10000)
toc()
rm(list=ls())
gc()


# Table S3
tic() # time scripts for each figure
source('TAB-voteshare-correlation.R', echo = T,  max.deparse.length=10000)
toc()
rm(list=ls())
gc()

# Table S4
tic() # time scripts for each figure
source('TAB-mover-neighborhoods.R', echo = T,  max.deparse.length=10000)
toc()
rm(list=ls())
gc()

# Figure S1
tic() # time scripts for each figure
source('EST-model-moving-2012-2016.R', echo = T,  max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-model-moving-2016-2020.R', echo = T,  max.deparse.length=10000)
rm(list=ls())
gc()
source('FIG-modeling-moving.R', echo = T,  max.deparse.length=10000)

toc()
rm(list=ls())
gc()

# Table S5
tic() # time scripts for each figure
# If Figure 4 has not yet been created, will need to run the following commented out analysis scripts to create SD output
# source('EST-current-results-2008-2012.R', echo = T,  max.deparse.length=10000)
# source('EST-current-results-2012-2016.R', echo = T,  max.deparse.length=10000)
# source('EST-current-results-2016-2020.R', echo = T,  max.deparse.length=10000)
# source('EST-current-results-2012-2016-pretrend.R', echo = T,  max.deparse.length=10000)
# source('EST-current-results-2016-2020-pretrend.R', echo = T,  max.deparse.length=10000)
source('TAB-within-strata-sd.R', echo = T,  max.deparse.length=10000)
toc()
rm(list=ls())
gc()

# Table S6
tic() # time scripts for each figure
source('TAB-party-covariate.R', echo = T,  max.deparse.length=10000)
toc()
rm(list=ls())
gc()


# Table S7
tic() # time scripts for each figure
source('TAB-seg-covariate.R', echo = T,  max.deparse.length=10000)
toc()
rm(list=ls())
gc()


# Table S8 
tic() # time scripts for each figure
source('TAB-current-effects-full.R', echo = T,  max.deparse.length=10000)
toc()
rm(list=ls())
gc()

# Table S9 (and S11)
tic() # time scripts for each figure
source('TAB-effect-size.R', echo = T,  max.deparse.length=10000)
toc()
rm(list=ls())
gc()


# Table S10 
tic() # time scripts for each figure
source('TAB-future-effects-full.R', echo = T,  max.deparse.length=10000)
toc()
rm(list=ls())
gc()

# Table S11 created previously(otherwise run the commented out scripts)
#source('TAB-effect-size.R', echo = T,  max.deparse.length=10000)


# Figure S2 already created previously (otherwise run the commented out scripts)
# source('EST-current-results-2016-2020-dwelling-age.R', echo = T,  max.deparse.length=10000)
# source('EST-current-results-2012-2016-dwelling-age.R', echo = T,  max.deparse.length=10000)
# source('FIG-current-results-age-dwelling.R', echo = T,  max.deparse.length=10000)


# Figure S3 
tic() # time scripts for each figure
source('EST-current-results-2016-2020-dwelling-age-white.R', echo = T,  max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-current-results-2012-2016-dwelling-age-white.R', echo = T,  max.deparse.length=10000)
rm(list=ls())
gc()
source('FIG-current-results-age-dwelling-whites.R', echo = T,  max.deparse.length=10000)

toc()
rm(list=ls())
gc()


# Figure S4
tic() # time scripts for each figure
source('EST-future-results-2012-2016-dwelling-age.R', echo = T,  max.deparse.length=10000)
rm(list=ls())
gc()
source('FIG-future-results-age-dwelling.R', echo = T,  max.deparse.length=10000)

toc()
rm(list=ls())
gc()

# Figure S5
tic() # time scripts for each figure
source('EST-future-results-2012-2016-dwelling-age-white.R', echo = T,  max.deparse.length=10000)
rm(list=ls())
gc()
source('FIG-future-results-age-dwelling-whites.R', echo = T,  max.deparse.length=10000)

toc()
rm(list=ls())
gc()

# Figure S6 previously created (otherwise run commented out scripts)
#tic() # time scripts for each figure
#source('EST-current-results-2012-2016-race-subset.R', echo = T,  max.deparse.length=10000)
#source('FIG-current-results-race-subset.R', echo = T,  max.deparse.length=10000)
#toc()
#gc()


# Figure S7 
tic() # time scripts for each figure
source('EST-future-results-2012-2016-race-subset.R', echo = T,  max.deparse.length=10000)
rm(list=ls())
gc()
source('FIG-future-results-race-subset.R', echo = T,  max.deparse.length=10000)
toc()
rm(list=ls())
gc()

# Figure S8 
tic() # time scripts for each figure
source('EST-future-results-2012-2016-by-district.R', echo = T,  max.deparse.length=10000)
rm(list=ls())
gc()
source('FIG-future-results-districts.R', echo = T,  max.deparse.length=10000)
toc()
rm(list=ls())
gc()

# Figure S9
tic() # time scripts for each figure 
# If figure 4 is not yet created, run commented out scripts below
#source('EST-current-results-2008-2012.R', echo = T,  max.deparse.length=10000)
#source('EST-current-results-2012-2016.R', echo = T,  max.deparse.length=10000)
#source('EST-current-results-2016-2020.R', echo = T,  max.deparse.length=10000)
source('EST-current-results-2008-by-year.R', echo = T,  max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-current-results-2012-by-year.R', echo = T,  max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-current-results-2016-by-year.R', echo = T,  max.deparse.length=10000)
rm(list=ls())
gc()
source('FIG-current-results-all-years.R', echo = T,  max.deparse.length=10000)
toc()
rm(list=ls())
gc()

# Figure S10
tic() # time scripts for each figure 
source('EST-current-results-2008-2012-by-state.R', echo = T,  max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-current-results-2012-2016-by-state.R', echo = T,  max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-current-results-2016-2020-by-state.R', echo = T,  max.deparse.length=10000)
rm(list=ls())
gc()
source('FIG-current-results-by-state.R', echo = T,  max.deparse.length=10000)
toc()
rm(list=ls())
gc()



# Table S12
tic() # time scripts for each figure 
# If figure 4 is not yet created, run commented out scripts below
#source('EST-current-results-2012-2016.R', echo = T,  max.deparse.length=10000)
#source('EST-current-results-2016-2020.R', echo = T,  max.deparse.length=10000)
#source('EST-current-results-2012-2016-pretrend.R', echo = T,  max.deparse.length=10000)
#source('EST-current-results-2016-2020-pretrend.R', echo = T,  max.deparse.length=10000)
source('EST-current-results-2012-2016-aspatial.R', echo = T, max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-current-results-2012-2016-block-group.R', echo = T, max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-current-results-2012-2016-block.R', echo = T, max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-current-results-2012-2016-full-knn.R', echo = T, max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-current-results-2012-2016-dr.R', echo = T, max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-current-results-2012-2016-k100.R', echo = T, max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-current-results-2012-2016-k500.R', echo = T, max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-current-results-2012-2016-d1600.R', echo = T, max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-current-results-2012-2016-new-neighbors.R', echo = T, max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-current-results-2012-2016-no-bg-control.R', echo = T, max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-current-results-2016-2020-aspatial.R', echo = T, max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-current-results-2016-2020-block-group.R', echo = T, max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-current-results-2016-2020-block.R', echo = T, max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-current-results-2016-2020-full-knn.R', echo = T, max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-current-results-2016-2020-dr.R', echo = T, max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-current-results-2016-2020-k100.R', echo = T, max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-current-results-2016-2020-k500.R', echo = T, max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-current-results-2016-2020-d1600.R', echo = T, max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-current-results-2016-2020-new-neighbors.R', echo = T, max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-current-results-2016-2020-no-bg-control.R', echo = T, max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-current-results-2016-2020-race-imps.R', echo = T, max.deparse.length=10000)
rm(list=ls())
gc()

source('TAB-alternative-effect-estimates.R', echo = T,  max.deparse.length=10000)
toc()
rm(list=ls())
gc()



# Table S13
tic() # time scripts for each figure 
# If figure 4 is not yet created, run commented out scripts below
#source('EST-future-results-2012-2016.R', echo = T,  max.deparse.length=10000)
#source('EST-future-results-2012-2016-pretrends.R', echo = T,  max.deparse.length=10000)
source('EST-future-results-2012-2016-aspatial.R', echo = T, max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-future-results-2012-2016-block-group.R', echo = T, max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-future-results-2012-2016-block.R', echo = T, max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-future-results-2012-2016-full-knn.R', echo = T, max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-future-results-2012-2016-dr.R', echo = T, max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-future-results-2012-2016-k100.R', echo = T, max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-future-results-2012-2016-k500.R', echo = T, max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-future-results-2012-2016-d1600.R', echo = T, max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-future-results-2012-2016-new-neighbors.R', echo = T, max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-future-results-2012-2016-no-bg-control.R', echo = T, max.deparse.length=10000)
rm(list=ls())
gc()

source('TAB-alternative-future-effect-estimates.R', echo = T,  max.deparse.length=10000)
toc()
rm(list=ls())
gc()


# Figure S11 
tic() # time scripts for each figure
source('EST-future-results-2008-2012-alt-1.R', echo = T,  max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-future-results-2012-2016-alt-1.R', echo = T,  max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-future-results-2008-2012-alt-2.R', echo = T,  max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-future-results-2012-2016-alt-2.R', echo = T,  max.deparse.length=10000)
rm(list=ls())
gc()

source('FIG-future-results-alternative-subsets.R', echo = T,  max.deparse.length=10000)
toc()
rm(list=ls())
gc()


# Table S14
tic() # time scripts for each figure
source('TAB-survey-population-comparison.R', echo = T,  max.deparse.length=10000)
toc()
rm(list=ls())
gc()

# Table S15 and S16 and S17
tic() # time scripts for each figure
# if Table 2 not already generated run commented out scripst below
#source('EST-survey-analysis-weights.R', echo = T,  max.deparse.length=10000)
#source('EST-survey-analysis-no-weights.R', echo = T,  max.deparse.length=10000)
source('TAB-survey-results-full.R', echo = T,  max.deparse.length=10000)
toc()
rm(list=ls())
gc()

# Table S18 and S19
tic() # time scripts for each figure
source('TAB-alt-tables.R', echo = T,  max.deparse.length=10000)
toc()
rm(list=ls())
gc()

# Figure S12
tic() # time scripts for each figure
source('FIG-party-switching-matrix-dedup.R', echo = T,  max.deparse.length=10000)
toc()
rm(list=ls())
gc()

# Figure S13
tic() # time scripts for each figure
source('FIG-distributions-dedup.R', echo = T,  max.deparse.length=10000)
toc()
rm(list=ls())
gc()


# Figure S14
# If Figure 4 has not yet been created, will need to run the following commented out analysis scripts to create SD output
tic() # time scripts for each figure
#source('EST-current-results-2008-2012.R', echo = T,  max.deparse.length=10000)
#source('EST-current-results-2012-2016.R', echo = T,  max.deparse.length=10000)
#source('EST-current-results-2016-2020.R', echo = T,  max.deparse.length=10000)
#source('EST-future-results-2008-2012.R', echo = T,  max.deparse.length=10000)
#source('EST-future-results-2012-2016.R', echo = T,  max.deparse.length=10000)
source('EST-current-results-2008-2012-dedup.R', echo = T,  max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-current-results-2012-2016-dedup.R', echo = T,  max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-current-results-2016-2020-dedup.R', echo = T,  max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-future-results-2008-2012-dedup.R', echo = T,  max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-future-results-2012-2016-dedup.R', echo = T,  max.deparse.length=10000)
rm(list=ls())
gc()
source('FIG-current-results-dedup.R', echo = T,  max.deparse.length=10000)
toc()
source('FIG-future-results-dedup.R', echo = T,  max.deparse.length=10000)
toc()
rm(list=ls())
gc()

# Table S20
tic() # time scripts for each figure
source('EST-survey-analysis-weights-dedup.R', echo = T,  max.deparse.length=10000)
rm(list=ls())
gc()
source('EST-survey-analysis-no-weights-dedup.R', echo = T,  max.deparse.length=10000)
rm(list=ls())
gc()
source('TAB-survey-results-dedup.R', echo = T,  max.deparse.length=10000)
toc()
rm(list=ls())
gc()

toc()

print(Sys.time())
####################
# Restore output to console
sink() 
sink(type="message")